package cn.wubo.multi.thread.demo03;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * TransactionTemplate编程式事务
 * 多线程事务回滚
 */
@Slf4j
@Service
public class Demo0304 {

    @Autowired
    private TransactionTemplate transactionTemplate;

    public void asyncSaveBatch() {
        // 线程个数
        final int SIZE = 6;
        //线程池
        final ExecutorService pool = Executors.newFixedThreadPool(SIZE);

        //new Thread(() -> {}).start();
        pool.execute(() -> {
            transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    /* 业务逻辑 */
                }
            });
        });
    }
}
